package com.sap.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sap.domain.User;
import com.sap.service.UserService;
import com.sap.mapper.UserMapper;
import org.springframework.stereotype.Service;

/**
 * @author luoh6
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2025-05-12 14:28:11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Override
    public void addUser() {
        User user = new User();
        user.setUsername("luoh7");
        user.setPassword("123456");
        this.save(user);
    }

    @Override
    @SentinelResource(value = "getUserById",blockHandler = "handleBlock",fallback = "handleFallback")
    public User getUserById(Long id) {
        User user = this.lambdaQuery().eq(User::getUsername, "user").one();
        return user;
    }

    // 熔断降级处理（参数需与原方法一致，末尾加 BlockException）
    public String handleBlock(Long id, BlockException ex) {
        return "Trigger Circuit Breaker: " + ex.getClass().getSimpleName();
    }

    // 通用异常处理（参数需与原方法一致，末尾加 Throwable）
    public String handleFallback(Long id, Throwable ex) {
        return "Fallback: " + ex.getMessage();
    }
}




